package org.example.service.impl;

import lombok.SneakyThrows;
import org.example.service.JobTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author: WangShengJie
 * @Date: 2025/8/1
 */
@Service
public class JobTaskServiceImpl implements JobTaskService {


    @Resource(name = "threadPoolExecutor")
    ThreadPoolTaskExecutor taskExecutor;

    public static CountDownLatch countDownLatch;

    @Override
    public void jobTest() throws InterruptedException {

        System.out.println("111111111111111111");
        List<String> list = Arrays.asList("11", "22", "33", "44", "55");
        countDownLatch = new CountDownLatch(list.size());
        for (; ; ) {
            for (String s : list) {
                taskExecutor.execute(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        System.out.println(s);
                        if (s.equals("33")) {
                            Thread.sleep(10);
                        }
                        countDownLatch.countDown();
                    }
                });
            }
            boolean awaitFlag = countDownLatch.await(30, TimeUnit.MINUTES);
            System.out.println("*********");
            if (!awaitFlag) {
                System.out.println("////////");
            }
        }
    }
}
